<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>函数节流</title>
  <style>
    #box {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: #eee;
    }
  </style>
</head>
<body>
  <div id="box"></div>
  <script>
    var box = document.querySelector('#box');
    
    box.onmousemove = throttle(() => {
      console.log(Date.now())
    }, 1000)

    function throttle(callback, time) {
      let timer = null
      return function() {
        if (timer) return
        timer = setTimeout(() => {
          callback()
          timer = null
        }, time)
      }
    }
  </script>
</body>
</html>